package com.yeyks.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.hotel.dal.domain.HotelCommission;
import com.yeyks.hotel.dal.dao.HotelCommissionMapper;
import com.yeyks.hotel.dal.domain.HotelCommissionBill;
import com.yeyks.hotel.param.HotelCommissionParam;
import com.yeyks.hotel.service.HotelCommissionBillService;
import com.yeyks.hotel.service.HotelCommissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.common.ajax.AjaxResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;


/**
 * <p>
 * 酒店佣金表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Slf4j
@Service
public class HotelCommissionServiceImpl extends ServiceImpl<HotelCommissionMapper, HotelCommission> implements HotelCommissionService {

    @Override
    public AjaxResponseBody getStatisticsData(Integer hotelId) {
        HotelCommission hotelCommissionEntitie = baseMapper.selectOne(new QueryWrapper<HotelCommission>()
                .eq(HotelCommission.HOTEL_ID, hotelId)//酒店id
                .eq(HotelCommission.TYPE, 1)//酒店佣金的类型为1 vip佣金
        );
        return AjaxResponseBody.success(hotelCommissionEntitie);
    }

    @Autowired
    private HotelCommissionBillService billService;

    //共享读写锁
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    /**
     * 给酒店用户增加佣金
     *
     * @param param
     * @return
     */
    @Override
    @Transactional
    public Boolean addAmount(HotelCommissionParam param) {
        if (param.getCommission() == null || param.getCommission() <= 0) {
            param.setCommission(0);
        }
        HotelCommission hotelCommission = getHotelCommission(param.getHotelId(), param.getType());
        try {
            lock.writeLock().lock();
            Integer totalAmount = hotelCommission.getTotalAmount() == null ? 0 : hotelCommission.getTotalAmount();
            Integer remainAmount = hotelCommission.getRemainAmount() == null ? 0 : hotelCommission.getRemainAmount();
            Integer waitAmount = hotelCommission.getWaitAmount() == null ? 0 : hotelCommission.getWaitAmount();
            hotelCommission.setTotalAmount(totalAmount + param.getCommission());
            hotelCommission.setRemainAmount(remainAmount + param.getCommission());
            hotelCommission.setWaitAmount(waitAmount + param.getCommission());
            hotelCommission.setUpdateTime(new Date());
            int res = baseMapper.updateById(hotelCommission);
            if (res != 1) {
                log.error("增加酒店佣金失败");
                return false;
            }
            HotelCommissionBill bill = getBill(hotelCommission, param);
            Integer billRes = billService.insert(bill);
            if (billRes != 1) {
                log.error("增加酒店佣金明细失败");
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("增加佣金金额出错{}", e.getLocalizedMessage());
        } finally {
            lock.writeLock().unlock();
        }
        return false;
    }

    private HotelCommissionBill getBill(HotelCommission hotelCommission, HotelCommissionParam param) {
        HotelCommissionBill bill = new HotelCommissionBill();
        bill.setOperatorAmount(param.getCommission());
        bill.setRemainAmount(hotelCommission.getRemainAmount());
        bill.setHotelId(hotelCommission.getHotelId());
        bill.setRemark(param.getBillRemark());
        bill.setType(param.getBillType());
        bill.setHotelCommissionId(hotelCommission.getId());
        bill.setCardId(param.getCardId());
        bill.setOrderNo(param.getOrderNo());
        bill.setStatus(param.getStatus());
        bill.setCreateTime(new Date());
        return bill;
    }

    private HotelCommission getHotelCommission(Integer hotelId, Integer type) {

        HotelCommission hotelCommission = baseMapper.selectByHotelId(hotelId, type);
        if (hotelCommission == null) {
            hotelCommission = create(hotelId, type);
            baseMapper.insert(hotelCommission);
        }
        return hotelCommission;
    }

    private HotelCommission create(Integer hotelId, Integer type) {
        HotelCommission hotelCommission = new HotelCommission();
        hotelCommission.setRemainAmount(0);
        hotelCommission.setTotalAmount(0);
        hotelCommission.setWaitAmount(0);
        hotelCommission.setType(type);
        hotelCommission.setHotelId(hotelId);
        Date date = new Date();
        hotelCommission.setCreateTime(date);
        hotelCommission.setUpdateTime(date);
        return hotelCommission;
    }

}
